layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate', 'jquery'], function (exports) {
    var $ = layui.$;
    var table = layui.table;
    var laypage = layui.laypage;
    var laydate = layui.laydate;
    var form = layui.form;
    zmnLayUtil.layFormVerify(form);
    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        page: false,
        height: 'full-140',
        defaultToolbar: [],
        done: function (res) {
            let table = layui.$('#zmn-table').next("div").find('table');
            let trs = table.find('tr');
            trs.each(function (i, item) {
                let tr = layui.$(item);
                let price = tr.find('td[data-field="price"]');
                let tdPrice = layui.$(price).data("edit", false).removeClass('layui-disabled').prop("disabled", false);
                tdPrice.find("input").prop("disabled", false).removeClass("text-disabled").addClass("text-enable");
            })

        }
    });
    // 提交
    form.on('submit(zmn-form-submit)', function (obj) {
        let formData = obj.field;
        let tableData = layui.table.cache['zmn-table'];
        let groupList = [];
        let priceStandard = formData.priceStandard;
        let reg = /^\d+(\.\d{1,2})?$/;
        let table = $("#zmn-table").next("div").find('table');
        //一口价销售价必填，非一口价价格区间要么填，要么不填，其余的可选
        for (let j = 0; j < tableData.length; j++) {
            let trData = tableData[j];
            let inner = new Object();
            let price = "";
            if(priceStandard==2){
                price = table.find("tr:eq(" + j + ") input[name=price]")[0].value;
            }
            let internalSettlementPrice = trData.internalSettlementPrice;
            let externalSettlementPrice = trData.externalSettlementPrice;
            let negotiableMinPrice ="";
            let negotiableMaxPrice ="";
            if(priceStandard==1){
                negotiableMinPrice = table.find("tr:eq(" + j + ") input[name=negotiableMinPrice]")[0].value;
                negotiableMaxPrice = table.find("tr:eq(" + j + ") input[name=negotiableMaxPrice]")[0].value;
            }

            //价格都没填写，忽略
            if (!price && !negotiableMaxPrice && !negotiableMinPrice && !internalSettlementPrice && !externalSettlementPrice) {
                continue;
            }
            //非一口价
            if (priceStandard == 1) {
                //只填写了后面部分
                if (!negotiableMaxPrice && !negotiableMinPrice) {
                    if (internalSettlementPrice || externalSettlementPrice) {
                        layer.alert("价格区间必填")
                        return false;
                    }
                }
                //区间只填写了部分
                if ((negotiableMaxPrice && !negotiableMinPrice) || (!negotiableMaxPrice && negotiableMinPrice)) {
                    layer.alert("价格区间必填");
                    return false;
                }
                //区间格式
                if (!reg.test(negotiableMaxPrice) || !reg.test(negotiableMinPrice)) {
                    layer.alert("价格区间格式有误，请填写正整数或者两位小数的正数");
                    return false;
                } else {
                    negotiableMaxPrice = negotiableMaxPrice * 100;
                    negotiableMinPrice = negotiableMinPrice * 100;
                    if (negotiableMaxPrice == '0' || negotiableMaxPrice == '0.0' || negotiableMaxPrice == '0.00') {
                        layer.alert("价格区间格式有误，请填写大于0的正整数或者两位小数的正数");
                        return false;
                    }
                    if (negotiableMinPrice == '0' || negotiableMinPrice == '0.0' || negotiableMinPrice == '0.00') {
                        layer.alert("价格区间格式有误，请填写大于0的正整数或者两位小数的正数");
                        return false;
                    }
                    if (negotiableMinPrice >= negotiableMaxPrice) {
                        layer.alert("价格区间格式有误，右侧必须大于左侧");
                        return false;
                    }
                }
            } else {
                //只填写了后面部分
                if (!price) {
                    if (internalSettlementPrice || externalSettlementPrice) {
                        layer.alert("销售价格必填")
                        return false;
                    }
                }
                //区间格式
                if (!reg.test(price)) {
                    layer.alert("销售价格格式有误，请填写正整数或者两位小数的正数");
                    return false;
                } else {
                    price = price * 100;
                }
            }
            //结算价格式
            if (internalSettlementPrice && !reg.test(internalSettlementPrice)) {
                layer.alert("内部结算价格式有误，请填写正整数或者两位小数的正数");
                return false;
            } else {
                internalSettlementPrice = internalSettlementPrice?internalSettlementPrice * 100:internalSettlementPrice;
            }
            if (externalSettlementPrice && !reg.test(externalSettlementPrice)) {
                layer.alert("外部结算价格式有误，请填写正整数或者两位小数的正数");
                return false;
            } else {
                externalSettlementPrice = externalSettlementPrice?externalSettlementPrice * 100:externalSettlementPrice;
            }
            inner["price"] = price;
            //城市分组id
            inner['groupId'] = trData.groupId;
            inner["internalSettlementPrice"] = internalSettlementPrice;
            inner["externalSettlementPrice"] = externalSettlementPrice;
            inner["negotiableMaxPrice"] = negotiableMaxPrice;
            inner["negotiableMinPrice"] = negotiableMinPrice;
            groupList.push(inner);
        }
        if (groupList.length <= 0) {
            layer.alert("请填写至少一个分组的价格数据");
            return false;
        }

        formData.groupList = groupList;
        var url = "/product/tariff/item/city/price/addCityPriceByCityGroup.action";
        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(formData),
            success: function (resp) {
                var message = resp.status == 200 ? "操作成功！" : resp.message;
                layer.msg(message);
                if (resp.status == 200) {
                    setTimeout(function () {
                        window.location.href = "/product/tariff/item/city/price/list.action?productId=" + json.vo.productId + "&tariffId=" + json.vo.tariffId + "&groupId=" + json.vo.groupId + "&itemId=" + json.vo.itemId + "&priceStandard=" + json.vo.priceStandard;
                    }, 1000);
                }
            },
            error: function (XMLHttpRequest, status, errorThrown) {
            }
        });
        return false;

    });


    laypage.render(layui.$.extend({limits: [10, 20, 30, 40, 50, 100]}, zmnPage.laypageOptions(json)));
    //对外暴露的接口
    exports('serv_product_tariff_item_city_price_citygrop_list', {});
});